// const number = parseInt(readline())
// const trains = readline().split(' ')
const number = 3
const trains = ['1', '2', '3']

const paths = []

function sortAll(vals = [], max = 0, inStack = [], outStack = []) {
  if (outStack.length === max) {
    paths.push(outStack.join(' '))
    return
  }
  if (vals.length === max) {
    inStack.push(vals.shift())
    sortAll(vals, max, inStack, outStack)
    return
  }
  if (vals.length > 0) {
    const cur = vals.shift()
    inStack.push(cur)
    sortAll(vals, max, inStack, outStack)
    inStack.pop()
    vals.unshift(cur)
  }
  if (inStack.length > 0) {
    const cur = inStack.pop()
    outStack.push(cur)
    sortAll(vals, max, inStack, outStack)
    outStack.pop()
    inStack.push(cur)
  }
}

sortAll(trains, number)
console.log(paths)

paths.sort((cur, prev) => {
  let i = 0
  while (i < cur.length) {
    if (cur[i] < prev[i]) {
      return -1
    } else if (cur[i] > prev[i]) {
      return 1
    }
    i++
  }
})

paths.forEach((item) => {
  console.log(item)
})
